package milu.servlets;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

import com.google.appengine.api.datastore.*;
import milu.*;
import milu.models.*;

public class UserPage extends HttpServlet {
    public void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

        String[] segments = request.getRequestURI().split("/");
        String userId = null;
        if(segments.length >= 3)
            userId = segments[2];

        // get this users info
        Key userKey = KeyFactory.createKey(MyUser.class.getSimpleName(), Long.parseLong(userId));
        MyUser thisUser = MyUser.getUserInfo(userKey);
        request.setAttribute("thisUser", thisUser);

        // get this user recipes
        List<Recipe> recipes = Recipe.getRecipes(userKey);
        request.setAttribute("recipes", recipes);

        request.getRequestDispatcher("/user-page.jsp").forward(request, response);
    }
}
